package com.zjitc.utils.old;


import com.zjitc.service.imp.OrderServiceImpl;
import org.apache.commons.codec.binary.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.Cookie;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * Create By IntelliJ IDEA
 *
 * @author: myq
 * @create-Time: 2017/11/6  15:11
 */
@Controller
public class Utils {
  /**
   * 全局
   */
  private static final String KEY_MD5 = "MD5";

  @Autowired
  private OrderServiceImpl service;


  /**
   * 随机获取一个uuid，通用唯一的识别码(Universally Unique Identifier)
   *
   * @return uuid的string类型
   */
  public static String getUUID() {
    return UUID.randomUUID().toString().replace("-", "");
  }

  /**
   * 利用MD5进行加密32位
   *
   * @param password 待加密的字符串
   * @return 加密后的字符串
   * @throws NoSuchAlgorithmException 没有这种产生消息摘要的算法
   */
  public static String encoderByMd5(String password)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    try {
      //得到一个信息摘要器
      MessageDigest md5 = MessageDigest.getInstance("md5");
      md5.update(password.getBytes());
      return new BigInteger(1, md5.digest()).toString(32);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * 生成含有随机盐的密码
   */
  public static String getSaltMD5(String password) {
    Random r = new Random();
    StringBuilder sb = new StringBuilder(16);
    sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
    int len = sb.length();
    if (len < 16) {
      for (int i = 0; i < 16 - len; i++) {
        sb.append("0");
      }
    }
    String salt = sb.toString(); //盐
    password = md5Hex(password + salt);
    char[] cs = new char[48];
    for (int i = 0; i < 48; i += 3) {
      cs[i] = password.charAt(i / 3 * 2);
      char c = salt.charAt(i / 3);
      cs[i + 1] = c;
      cs[i + 2] = password.charAt(i / 3 * 2 + 1);
    }
    return new String(cs);
  }
  /**
   * 获取十六进制字符串形式的MD5摘要
   */
  public static String md5Hex(String src) {
    try {
      MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
      byte[] bs = md5.digest(src.getBytes());
      return new String(new Hex().encode(bs));
    } catch (Exception e) {
      return null;
    }
  }


  /**
   * 校验密码是否正确
   * @param password
   * @param md5  加密后的密码
   * @return
   */
  public static boolean checkPassword(String password, String md5) {
    char[] cs1 = new char[32];
    char[] cs2 = new char[16];
    for (int i = 0; i < 48; i += 3) {
      cs1[i / 3 * 2] = md5.charAt(i);
      cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
      cs2[i / 3] = md5.charAt(i + 1);
    }
    String salt = new String(cs2);
    return md5Hex(password + salt).equals(new String(cs1));
  }




  public static Cookie getCookieByName(String name, Cookie[] cookies) {
    if (cookies != null) {
      for (Cookie c : cookies) {
        if (name.equals(c.getName())) {
          return c;
        }
      }
    }
    return null;
  }


  /**
   * 随机生成快递单号
   */
  public static String code() {
    String s = "0123456789";
    Random random = new Random();
    String msg = "389";
    for (int i = 0; i < MyNumber.TEN.ordinal(); i++) {
      int index = random.nextInt(10);
      String content = String.valueOf(s.charAt(index));
      msg += content;
      double theta = random.nextInt(45) * Math.PI / 180;
    }
    return msg;
  }




  /**
   * 模糊查询
   */
  public static <T> List fuzzysearch(String fuzzy, List<T> listGetAll) {
    return null;
  }

  /**
   * 获得包含字母的6位数字验证码
   */
  public static String forgetcode() {
    String s = "ABCDEFGHGKLMNPQRSTUVWXYZ23456789";
    Random random = new Random();
    //session中要用到
    String msg = "";
    int x = 5;
    for (int i = 0; i < MyNumber.FIVE.ordinal(); i++) {
      int index = random.nextInt(32);
      String content = String.valueOf(s.charAt(index));
      msg += content;
    }
    return msg;
  }

  /**
   * 获得6位随机数字验证码
   * @throws NoSuchAlgorithmException 没有这种产生消息摘要的算法
   */
  public static String getCode(){
    Random random = new Random();
    String code = "";
    for(int i = 0;i < 6 ;i++){
      int count = random.nextInt(9)+1;
      code += count;
    }
    return code;
  }


  public static String Dateformat(Date date){
    DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String format = dateformat.format(date);
    return format;
  }

  /**
   *手机保密措施
   * @param phone
   * @return
   */
  public static String confident(String phone) {
    String start = phone.substring(0, 3);
    String end = phone.substring(phone.length() - 4);
    return start + "****" + end;
  }
}
